<template>
  <div class="login-bg">
    <div class="login-box">
      <h2 class="login-tit">My Vue2</h2>
      <el-form :model="formData" ref="formData" @keyup.enter.native="submit">
        <el-form-item prop="userName" :rules="[V.RI, V.space]">
          <el-input v-model="formData.userName" clearable prefix-icon="el-icon-user" placeholder="请输入用户名" autofocus></el-input>
        </el-form-item>
        <el-form-item prop="password" :rules="[V.RI, V.space]">
          <el-input v-model="formData.password" clearable prefix-icon="el-icon-unlock" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
        <p>用户名密码随意填写即可！</p>
        <el-button class="btn-submit" type="primary" @click="submit" :loading="loading">登 录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import V from '@/assets/scripts/validate'

export default {
  data() {
    return {
      V,
      loading: false,
      // 表单相关
      formData: {
        userName: '',
        password: ''
      }
    }
  },
  mounted() {
  },
  methods: {
    /**
     * 登录
     */
    submit() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store.commit('SET_USER', { userName: this.formData.userName })
          this.$router.replace('/home')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-bg {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: #f7f7f7;
}
.login-box {
  width: 400px;
  padding: 30px 20px;
  background: #fff;
  border-radius: 12px;
  .login-tit {
    margin-bottom: 20px;
    text-align: center;
  }
  .btn-submit {
    width: 100%;
  }
}
</style>